#include "star.h"


starobject::starobject(){
    url="about:blank";
    text="<空>";
    time="null";
}

star::star(){
    if(QSqlDatabase::contains("qt_sql_default_connection"))
        db = QSqlDatabase::database("qt_sql_default_connection");
    else
        db = QSqlDatabase::addDatabase("QSQLITE");
//    db.setHostName("127.0.0.1");
//    db.setUserName("root");
//    db.setPassword("");
    db.setDatabaseName("../info.db");
//    db.open();
    if(!db.open())
    {
        QMessageBox::critical(0,"错误","无法连接",QMessageBox::Yes);
    }
    query=QSqlQuery(db);
    QString sql=QString("create table user("
                        "account varchar(32) not null primary key,"
                        "username varchar(32) not null,"
                        "psw varchar(32) not null"
                        ")");
    query.exec(sql);
    query.exec("create table mark("
               "mark_id int,"
               "account varchar(32) not null,"
               "url varchar(256),"
               "mark_text varchar(256),"
               "time datetime primary key"
               ")");

    QString sql2 = QString("create table if not exists download("
                        "downloadFile varchar(255) not null primary key, "
                        "downloadFileName varchar(255) not null,"
                        "downloadFileDir varchar(255) not null,"
                        "url varchar(255) not null,"
                        "curTime varchar(63) not null,"
                        "totalBytes DOUBLE not null,"
                        "currentState varchar(20) not null,"
                        "account varchar(31) not null"
                        ")");
    if(query.exec(sql2))
        qDebug()<<"download表建立成功！！";
    else
        qDebug()<<"表建立失败！！！！！";

    if(query.exec("insert into download values('DICTIONARY','none','NoDownloadDir','none','none',0,'none','none');"))
        qDebug()<<"dir插入成功！！";
    else
        qDebug()<<"dir插入失败！！";

    for(int i=0;i<5;i++){
        liststar[i]=new starobject();
    }
    for(int i=0;i<100;i++){
        starlist[i]=new starobject();
    }
    isadd=0;
}

QString star::getTime(){
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_date =current_date_time.toString("yyyy-MM-dd hh:mm:ss");
    return current_date;
}

void star::slotCountMessage()
{
    if(net!=ui->tab->getUrl())
    {
        net=ui->tab->getUrl();
        isAdded(net);
    }
}


bool star::isAdded(QString url)
{
    if(flag==0){
        QTimer *timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(slotCountMessage()));
        timer->start(1000);
        flag++;
    }
    QString sql1=QString("select * from mark where account = '%1' and url='%2'").arg(ACCOUNT,url);
   // query.prepare(sql1);
    if(query.exec(sql1)){

        if(query.next()){
            isadd=1;
            ui->favorite->setIcon(QIcon(":/resource/imgs/add_to_favorites_52px.png"));
            return 1;
        }
        else {
            isadd=0;
            ui->favorite->setIcon(QIcon(":/resource/imgs/add_to_favorites_64px.png"));
            return 0;
        }
    }
    return 0;

}

void star::AddStar(QString url,QString text)
{

    if(!isAdded(url)){

        //qDebug()<<isAdded(url);
        QString sql = QString("insert into mark "
                                  "VALUES (null,'%1','%2','%3','%4')")
                    .arg(ACCOUNT,url,text,getTime());
       // query.prepare(sql);
            if(query.exec(sql))
            {
            }else
            {
//                QMessageBox::information(NULL, "infor", "failed!!");
                qDebug()<<query.lastError();
            }
        }



}

void star::DeleteStar(QString url)
{
    QString sql=QString("delete from mark where account = '%1' and url = '%2'").arg(ACCOUNT,url);
    //query.prepare(sql);
    if(query.exec(sql))
    {

    }
    else{
//        QMessageBox::information(NULL, "infor", "failed!!");
        qDebug()<<query.lastError();
    }
}

void star::on_button_addstar_clicked()
{

    if(isadd==0)
    {

    AddStar(ui->tab->getUrl(),ui->tab->getTitle());
//    ui->favorite->setText("-");
    ui->favorite->setIcon(QIcon(":/resource/imgs/add_to_favorites_52px.png"));
    isadd=1;
    }
    else if(isadd==1)
    {
        DeleteStar(ui->tab->getUrl());
//        ui->favorite->setText("+");
        ui->favorite->setIcon(QIcon(":/resource/imgs/add_to_favorites_64px.png"));
        isadd=0;
    }
}

void star::loadstar(){
    QString sql1=QString("select * from mark where account='%1' order by time desc").arg(ACCOUNT);

    //query.prepare(sql1);
        int i=0;

        if(query.exec(sql1))
        {

            while(query.next()&&i<5)
            {

               liststar[i]->url= query.value("url").toString();
               liststar[i]->text=query.value("mark_text").toString();
               liststar[i]->time=query.value("time").toString();

               i++;
            }
            while(i<5){
                liststar[i]=new starobject();
                i++;
            }


        }else
        {

            qDebug()<<query.lastError();
        }

}

void star::loadstar1(){
    QString sql1=QString("select *from mark where account='%1' order by time desc").arg(ACCOUNT);
//    QMessageBox::information(NULL, "infor", sql1);
   // query.prepare(sql1);
        int i=0;

        if(query.exec(sql1))
        {

            while(query.next()&&i<100)
            {

               starlist[i]->url= query.value("url").toString();
               starlist[i]->text=query.value("mark_text").toString();
               starlist[i]->time=query.value("time").toString();

               i++;
            }
            if(i<100)
                starlist[i]=new starobject();

        }else
        {
//            QMessageBox::information(NULL, "infor", "failed!!");
            qDebug()<<query.lastError();
        }

}

void star::deletenet(QString url){
    QString sql1=QString("delete from mark where account = '%1' and url = '%2'").arg(ACCOUNT,url);
    //query.prepare(sql1);

        if(query.exec(sql1))
        {
        }else
        {
//            QMessageBox::information(NULL, "infor", "failed!!");
            qDebug()<<query.lastError();
        }
}
